package com.linrui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController
public class TestController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/")
    public String get(){
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://localhost:8081/user/1", String.class);

        String json= JSON.toJSONString(forEntity);

        JSONObject object = JSONObject
                .parseObject(json);
        return object.getString("body");
    }
    @GetMapping("/stock")
    public Object stock(){

        Map<String,String> mp =new HashMap<>();
        mp.put("api_name","fund_nav");
        mp.put("token","531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a");
        //mp.put("params"," {\"list_stauts\":\"L\"}");

        // mp.put("fields","ts_code,name,area,industry,list_date");
        mp.put("Content-type","application/json;charset=utf8");
        Object responseVo = restTemplate.postForObject("http://api.waditu.com", mp, Object.class);
        System.out.println(responseVo);
        return responseVo;


//        String json= JSON.toJSONString(forEntity);
//
//        return unicodeToString(forEntity);
    }
    @GetMapping("/stocks")
    public Object stocks(){
        Map<String,Object> mp =new HashMap<>();
        mp.put("api_name","daily");
        mp.put("token","531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a");
        Map<String,String> mp2 =new HashMap<>();
        mp2.put("ts_code","000002.SZ");
        mp2.put("start_date","19980101");
        mp2.put("end_date","20210903");
        // mp2.put();
        mp.put("params",mp2);

        // mp.put("fields","ts_code,name,area,industry,list_date");
        mp.put("Content-type","application/json;charset=utf8");
        Object responseVo = restTemplate.postForObject("http://api.waditu.com", mp, Object.class);
        //System.out.println(responseVo.getString("data"));

        return responseVo;//.getString("data");

    }
    public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuilder buffer = new StringBuilder();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(Character.toString(letter));
            start = end;
        }
        return buffer.toString();
    }


    static String getUnicode(String s) {
        try {
            StringBuffer out = new StringBuffer("");
            byte[] bytes = s.getBytes("unicode");
            for (int i = 0; i < bytes.length - 1; i += 2) {
                out.append("\\u");
                String str = Integer.toHexString(bytes[i + 1] & 0xff);
                for (int j = str.length(); j < 2; j++) {
                    out.append("0");
                }
                String str1 = Integer.toHexString(bytes[i] & 0xff);
                out.append(str1);
                out.append(str);

            }
            return out.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String unicodeToString(String str) {

        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");
        }
        return str;
    }

}
